﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLWZGao6.Domain.Common;
using WLWZGao6.Domain.IRepository;
using WLWZGao6.Domain.Model;
using WLWZGao6.EndCode;
using WLWZGao6.Domain.Dto;
using Microsoft.EntityFrameworkCore;

namespace WLWZGao6.EndCode.Repository
{
    public class RoleRepository : IRoleRepository
    {
        public UserDataAccess _userDataAccess;
        public RoleRepository(UserDataAccess userDataAccess)
        {
            _userDataAccess = userDataAccess;
        }
        //角色添加
        public EData RoleAdd(Role role)
        {
            _userDataAccess.Role.Add(role);
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "添加成功!" };
        }
        //角色删除
        public EData RoleDel(int Ids)
        {
            var ss = _userDataAccess.Role.Where(x => x.Id == Ids).ToList();
            _userDataAccess.Role.RemoveRange(ss);
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "删除成功!" };
        }
        //角色显示
        public EData RoleShow(string RoleName, int Index, int Size)
        {
            var ss = _userDataAccess.Role.ToList();
            var dd = ss.Count();
            var list = ss.OrderBy(x => x.Id).Skip((Index - 1) * Size).Take(Size).ToList();
            if (!string.IsNullOrEmpty(RoleName))
            {
                ss = ss.Where(x => x.RoleName.Contains(RoleName)).ToList();
                dd = ss.Count();
                list = ss.OrderBy(x => x.Id).Skip((Index - 1) * Size).Take(Size).ToList();
            }
            return new EData() { Code = Endata.Success, Msg = "显示成功!", Data =new { List=list,Count=dd} };
        }
        //角色修改
        public EData RoleUpd(int Ids, Role role)
        {
            var ss = _userDataAccess.Role.Where(x => x.Id == Ids).SingleOrDefault();
            ss = role;
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "修改成功!" };
        }
        //用户角色添加
        public EData UserRoleAdd(UserRole userRole)
        {
            _userDataAccess.userRole.Add(userRole);
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "添加成功!" };
        }
        //用户角色删除
        public EData UserRoleDel(int Ids)
        {
            var ss = _userDataAccess.userRole.Where(x => x.Id == Ids).SingleOrDefault();
            _userDataAccess.userRole.RemoveRange(ss);
            return new EData() { Code = Endata.Success, Msg = "删除成功!" };
        }

        //角色权限显示
        public EData RolePermShow()
        {
            var dd = _userDataAccess.Role.ToList();
            return new EData() { Code = Endata.Success, Msg = "显示成功!", Data = dd };
        }
        //角色权限添加
        public EData RolePermAdd(RolePremDto rolePremDto)
        {
            int[] permid = rolePremDto.PermId;
            Role role = rolePremDto.Role;

            _userDataAccess.Role.Add(role);
            _userDataAccess.SaveChanges();

            List<RolePerm> list = new();
            for (int i = 0; i < permid.Length; i++)
            {
                RolePerm rp = new RolePerm();
                rp.RoleId = role.Id;
                rp.PermId = permid[i];
                list.Add(rp);
            }
            _userDataAccess.RolePerm.AddRange(list);
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "添加成功" };
        }
        //角色权限编辑
        public EData RolePermUpt(int Ids, string PermId)
        {
            //没有使用事务
            var ss = _userDataAccess.RolePerm.Where(x => x.RoleId == Ids).ToList();
            _userDataAccess.RolePerm.RemoveRange(ss);
            _userDataAccess.SaveChanges();

            var dd = PermId.Split(',');
            foreach (var item in dd)
            {
                int sta = _userDataAccess.Database.ExecuteSqlInterpolated($"insert into RolePerm values({Ids},{item})");
            }
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "编辑成功!" };

        }
        //角色权限删除
        public EData RolePermDel(int Ids)
        {
            var ss = _userDataAccess.RolePerm.Where(x => x.RoleId == Ids).ToList();
            var dd = _userDataAccess.Role.Where(x => x.Id == Ids).ToList();
            _userDataAccess.Role.RemoveRange(dd);
            _userDataAccess.SaveChanges();
            _userDataAccess.RolePerm.RemoveRange(ss);
            _userDataAccess.SaveChanges();
            return new EData() { Code = Endata.Success, Msg = "删除成功!" };
        }
        /// <summary>
        /// 递归权限显示
        /// </summary>
        /// <returns></returns>
        public List<PermDto> PermShow()
        {
            List<Perm> list = _userDataAccess.Perm.ToList();
            List<PermDto> stlist = PermTypeShow(list);
            return stlist;
        }
        public List<PermDto> PermTypeShow(List<Perm> perms, int Ids = 0)
        {
            var list = perms.Where(x => x.PId == Ids).ToList();
            if(perms.Count()==0)
            {
                return null;
            }
            List<PermDto> juese = new List<PermDto>();
            foreach (var item in list)
            {
                PermDto m = new PermDto();
                m.Id = item.Id;
                m.PermName = item.PermName;
                m.Children = PermTypeShow(perms, item.Id);
                juese.Add(m);
            }
            return juese;
        }
    }
}
